package com.neusoft.mall.view;

import com.neusoft.mall.controller.ProductController;
import com.neusoft.mall.model.Product;

import java.util.List;
import java.util.Scanner;

public class MyView {
    private static final String CONTEXT = "欢迎来到商品管理系统：\n" +
            "下面是商品的功能列表：\n" +
            "[QUERY/Q]:查看全部商品的信息\n" +
            "[GET/G]:查看某商品的详细信息\n" +
            "[SEARCH/S]:查询商品信息(根据商品名来查询)\n" +
            "[DELETE/D]:删除商品信息\n" +
            "[ADD/A]:添加商品信息\n" +
            "[UPDATE/U]:更新商品信息\n" +
            "[MAIN/M]:返回主菜单\n" +
            "[EXIT/E]:退出商品管理系统\n";

    public static ProductController productController = new ProductController();
    public static void handleQKey(){
        List<Product> products = productController.queryAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }
    public static void handleKeyG(){
        System.out.print("请输入商品【Id】:");
        Integer id;
        Product product;
        while(scan.hasNext()) {
            try {
                id = Integer.valueOf(scan.next());
            } catch (Exception e) {
                System.out.println("请输入正确的id:" + e);
                continue;
            }

            product = productController.queryProById(id);
            //ifn
            if (product == null) {
                System.out.println("商品不存在！");
            } else
                System.out.println(product);
            break;
        }
    }

    static void handleKdyD(){
        System.out.print("请输入要删除的商品【Id】:");
        Integer id;
        Product product;
        while (scan.hasNext()) {
            try {
                id = Integer.valueOf(scan.next());
            } catch (Exception e) {
                System.out.println("请输入正确的id:");
                continue;
            }

            Boolean flag = productController.delPro(id);
            if (flag) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
            break;
        }
    }

    private static Scanner scan = new Scanner(System.in);

    public static void main( String[] args ) {

        System.out.println(CONTEXT);

            while(scan.hasNext()){
                String in = scan.next();
                in = in.toUpperCase();

                switch (in){
                    case "E":{
                        System.out.println("系统退出！！");
                        return;
                    }
                    case "M":{
                        System.out.println(CONTEXT);
                        System.out.println("回到主界面！");
                        break;
                    }
                    case "Q":{
                        handleQKey();
                        break;
                    }
                    case "G":{
                        handleKeyG();
                        break;
                    }
                    case "D":{
                        handleKdyD();
                        break;
                    }
                }
            }
    }
}
